package com.wyj.rpc.service.impl;

import com.wyj.rpc.anno.RpcService;
import com.wyj.rpc.pojo.User;
import com.wyj.rpc.service.IUserService;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;

/**
 * TODO: 用户服务
 *
 * @author wuyijie
 * @date 2021/12/26 11:16 上午
 */
@RpcService
@Service
public class UserServiceImpl implements IUserService {

    private final static Map<Integer, User> USER_MAP = new HashMap<>(4);

    @PostConstruct
    public void init() {
        USER_MAP.put(1, new User(1, "张三", 21));
        USER_MAP.put(2, new User(2, "李四", 22));
        USER_MAP.put(3, new User(3, "王五", 23));
        USER_MAP.put(4, new User(4, "赵六", 24));
    }

    /**
     * TODO: 根据id查找用户信息
     *
     * @param id id
     * @return com.wyj.rpc.pojo.User
     * @author wuyijie
     * @date 2021/12/26 11:20 上午
     */
    @Override
    public User findById(Integer id) {
        if (!USER_MAP.containsKey(id)) {
            throw new RuntimeException("provider无法找到该id的用户");
        }
        return USER_MAP.get(id);
    }
}
